<?php

namespace App\Http\Controllers\Admin;

use App\Events\PermChangeEvent;
use Illuminate\Http\Request;
use App\Http\Requests\PermissionCreateRequest;
use App\Http\Requests\PermissionUpdateRequest;

use App\Http\Requests;
use App\Http\Controllers\BaseController;
use Illuminate\Support\Facades\DB;
use App\Models\Permission;
use Cache, Event;

class PermissionController extends BaseController
{
    protected $fields = [
        'name' => '',
        'display_name' => '',
        'description' => '',
        'cid' => 0,
        'icon' => '',
    ];

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, $cid = 0)
    {
        $datas = array();
        $cid = (int)$cid;
        $datas['cid'] = $cid;
        $datas['data'] = DB::table('permissions')->where('cid', '=', $cid)->orderBy('listorder', 'asc')->paginate(15);
        if ($cid > 0) {
            $datas['curr'] = Permission::find($cid);
        }
        return view('admin.permission.index', compact('datas', $datas));
    }

    //菜单排序
    public function listorder(Request $request)
    {
        $cid = $request->get('cid');
        $ids = $request->get('listorders');
        foreach ($ids as $key => $val) {
            $permission = Permission::find((int)$key);
            $permission->listorder = $val;
            $permission->save();
        }
        Event::fire(new PermChangeEvent());
        return redirect('admin/permission/' . $cid)->withSuccess('排序成功！');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($cid)
    {
        $data = [];
        foreach ($this->fields as $key => $val) {
            $data[$key] = old($key, $val);
        }
        $data['cid'] = (int)$cid;
        return view('admin.permission.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(PermissionCreateRequest $request)
    {
        $permission = new Permission();
        foreach (array_keys($this->fields) as $field) {
            $permission->$field = $request->get($field);
        }
        $permission->save();
        Event::fire(new PermChangeEvent());
        return redirect('admin/permission/' . $permission->cid)->withSuccess('添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $permission = Permission::find((int)$id);
        if (!$permission) return redirect('/admin/permission')->withErrors("找不到该权限!");
        $data = ['id' => (int)$id];
        foreach (array_keys($this->fields) as $field) {
            $data[$field] = old($field, $permission->$field);
        }
        return view('admin.permission.edit', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function update(PermissionUpdateRequest $request, $id)
    {
        $permission = Permission::find((int)$id);
        foreach (array_keys($this->fields) as $field) {
            $permission->$field = $request->get($field);
        }

        $permission->save();
        Event::fire(new PermChangeEvent());
        return redirect('admin/permission/' . $permission->cid)->withSuccess('修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $child = Permission::where('cid', $id)->first();
        if ($child) {
            return redirect()->back()->withErrors('请先将该权限的子权限删除再操作！');
        }
        $res = Permission::find((int)$id);
        if ($res) {
            $res->delete();
        } else {
            return redirect()->back()->withErrors('删除失败');
        }
        Event::fire(new PermChangeEvent());
        return redirect()->back()->withSuccess('删除成功');
    }
}
